
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;

using Swaf;
using Swaf.BizObj;
using Swaf.Gui;

[assembly: WebResource("Articles.Gui.AspNet.script.ArtForms.js", "application/x-javascript")]
namespace Articles.Gui.AspNet
{
    public class ArticleTranslationDisplay : Syrinx.Gui.AspNet.Nitobi2.NitobiBizObjDisplay, IUserFormDisplay
	{
        protected string m_userFormXml = null;
		protected string m_translationTemplate;
		public ArticleTranslationDisplay() : base() { }
		public ArticleTranslationDisplay(ActiveWidget widget) : base(widget) { }
		protected override bool processControl(Swaf.BizObj.IBizObj obj, System.Web.UI.Control ctl, string fldName, ref string fldValue)
		{
			if (fldName.Equals("titleTranslation"))
			{
				IBizObjList list = obj.get("translatedTitles") as IBizObjList;
				fldValue = getFieldValue(list, obj.get("translationId") as Swaf.BGuid.Guid);
				obj.put("titleTranslation", fldValue);
			}
			else if (fldName.Equals("summaryTranslation"))
			{
				IBizObjList list = obj.get("translatedSummaries") as IBizObjList;
				fldValue = getFieldValue(list, obj.get("translationId") as Swaf.BGuid.Guid);
				obj.put("summaryTranslation", fldValue);
			}
			else if (fldName.Equals("bodyTranslation"))
			{
				IBizObjList list = obj.get("translatedBodies") as IBizObjList;
				fldValue = getFieldValue(list, obj.get("translationId") as Swaf.BGuid.Guid);
				obj.put("bodyTranslation", fldValue);
			}
			else
				return base.processControl(obj,ctl,fldName,ref fldValue);
			return true;
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			if (State.State.get("m_translationTemplate") == null)
				State.State.put("m_translationTemplate", m_translationTemplate);
		}

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            string scriptUrl = Page.ClientScript.GetWebResourceUrl(typeof(ArticleTranslationDisplay), "Articles.Gui.AspNet.script.ArtForms.js");
            Page.ClientScript.RegisterClientScriptInclude("Articles.ArtFormScript", scriptUrl);
            if(m_userFormXml != null)
                Page.ClientScript.RegisterClientScriptBlock(typeof(ArticleTranslationDisplay), ClientID,
                    string.Format("if(window.attachEvent){{window.attachEvent( 'onload', function(){{showArtFormData($({0}), '{1}');}} );}}else{{window.addEventListener('load', function(){{showArtFormData($({0}), '{1}');}},false);}}",
                        ClientID, m_userFormXml), true);
        }

		public override string ContentName
		{
			get
			{
				if (EditPrivilege == Swaf.Security.Privileges.ReadOnly)
					m_app.sessionData["TranslationMode"] = null;

				if (m_app.sessionData["TranslationMode"] is bool && (bool)m_app.sessionData["TranslationMode"])
					return TranslationTemplate;
				return base.ContentName;
			}
		}

		public string TranslationTemplate
		{
			get { return (string)getOffline("m_translationTemplate"); }
			set { putOffline("m_translationTemplate", value); }
		}

        public void showArtFormData(object xml)
        {
            if(xml != null)
                m_userFormXml = xml.ToString();

        }

        public override void getState(System.Xml.XmlElement clientState, string pageHtml)
        {
            base.getState(clientState, pageHtml);
            if (m_userFormXml != null)
                clientState.SetAttribute("formData", m_userFormXml);
        }

		protected string getFieldValue(IBizObjList list, Swaf.BGuid.Guid translationId)
		{
			//ActiveBizProc proc = BPC.getActiveBizProc(State.ParentBizProcIds[0]);
			//Swaf.BGuid.Guid id = proc.get("locId") as Swaf.BGuid.Guid;
			if (translationId != null)
			{
				IBizObj body = list.search("transId", translationId);
				if (body != null)
					return body.getItem("val") as string;
			}
			return "";
		}
	}
}
